package cn.gok.web;

import cn.gok.entity.Student;
import cn.gok.entity.Teacher;
import cn.gok.service.NewTeacherService;
import cn.gok.service.impl.NewTeacherServiceImpl;
import cn.gok.util.R;
import cn.hutool.json.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/newTeacher")
public class NewTeacherServlet extends HttpServlet {

    private NewTeacherService newTeacherService=new NewTeacherServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        switch (action){
            case "getTeacherInfo":getStudentInfo(req,resp);break;//查询教师信息
            case "checkPassword":checkPassword(req, resp);break;//校验密码
            case "updatePassword":updatePassword(req, resp);break;//修改密码
            case "insertCourse":insertCourse(req, resp);break;//添加课程
            case "selectCourse":selectCourse(req, resp);break;//查询课程列表
            case "deleteCourse":deleteCourse(req, resp);break;//删除课程
            case "selectStudent":selectStudent(req, resp);break;//查询学生
            case "updateScore":updateScore(req, resp);break;//课程评分

        }
    }
    //查询教师信息
    public void getStudentInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Teacher teacher =(Teacher) req.getSession().getAttribute("teacher");
        R data = R.create(teacher);
        resp.getWriter().println(JSONUtil.toJsonStr(data));
    }

    //校验密码
    public void checkPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Teacher teacher = (Teacher) req.getSession().getAttribute("teacher");
        String password = req.getParameter("password");
        String teaId = String.valueOf(teacher.getTeaId());
        R r = newTeacherService.checkPassword(password,teaId );
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }
    /*
     * 修改密码
     * */
    public void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Teacher teacher =(Teacher) req.getSession().getAttribute("teacher");
        String  teaId = String.valueOf(teacher.getTeaId());
        String password = req.getParameter("password");
        R r = newTeacherService.changePassword(password, teaId);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);
    }

    //添加课程
    public void insertCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Teacher teacher = (Teacher) req.getSession().getAttribute("teacher");
        String teaName = teacher.getTeaName();
        String  teaId = String.valueOf(teacher.getTeaId());
        String cou_name = req.getParameter("cou_name");
        String starttime = req.getParameter("starttime");
        String place = req.getParameter("place");
        String max = req.getParameter("max");
        R r = newTeacherService.insertCourse(cou_name, starttime, place, max, teaName, teaId);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

    //查询课程列表
    public void selectCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Teacher teacher = (Teacher) req.getSession().getAttribute("teacher");

        String  teaId = String.valueOf(teacher.getTeaId());
        String couName = req.getParameter("couName");
        String place = req.getParameter("place");
        String state = req.getParameter("state");

        int currPage = Integer.parseInt(req.getParameter("currPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        R r = newTeacherService.selectCourse(couName, place, state, teaId, currPage, pageSize);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

    //取消选课
    public void deleteCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Teacher teacher = (Teacher) req.getSession().getAttribute("teacher");

        String  teaId = String.valueOf(teacher.getTeaId());
        String couId = req.getParameter("couId");
        R r = newTeacherService.deleteCourse(couId, teaId);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

    //查询学生
    public void selectStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String couId = req.getParameter("couId");
        R r = newTeacherService.selectStudent(couId, 1, 5);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

    //课程评分
    public void updateScore(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String couId = req.getParameter("couId");
        String stuId = req.getParameter("stuId");
        String scores = req.getParameter("scores");
        R r = newTeacherService.updateScores(couId, stuId, scores);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }


}
